home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 7675 / 7675.xpi / chrome / wikilook / content / en.wiktionary.js < prev    next >
Text File  |  2009-11-01  |  13KB  |  307 lines

  1. const WL_Wiktionary_section_names = [
  2.     "Abbreviation",
  3.     "Acronym",
  4.     "Adjective",
  5.     "Adverb",
  6.     "Anagrams",
  7.     "Article",
  8.     "Cardinal number",
  9.     "Cardinal numeral",
  10.     "Compounds",
  11.     "Conjugation",
  12.     "Conjunction",
  13.     "Contraction",
  14.     "Counter",
  15.     "Declension",
  16.     "Descendants",
  17.     "Determiner",
  18.     "Idiom",
  19.     "Infix",
  20.     "Inflection",
  21.     "Initialism",
  22.     "Interjection",
  23.     "Noun",
  24.     "Number",
  25.     "Numeral",
  26.     "Ordinal number",
  27.     "Ordinal numeral",
  28.     "Participle",
  29.     "Particle",
  30.     "Phrase",
  31.     "Pinyin syllable",
  32.     "Prefix",
  33.     "Preposition",
  34.     "Pronoun",
  35.     "Proper noun",
  36.     "Proverb",
  37.     "Quotations",
  38.     "Romaji",
  39.     "Trivia",
  40.     "Verb",
  41.     "Verb form",
  42.     "Han character",
  43.     "Hiragana character",
  44.     "Katakana character",
  45. ];
  46.  
  47. /**
  48. * Create a new English Wiktionary handler and tie it to the WikiLook interface
  49. **/
  50. function WikiLook_en (WikiLook) {
  51.   
  52.     this.WL = WikiLook
  53.   
  54. }
  55.  
  56.  
  57. WikiLook_en.prototype.responseFetchReal = function (response) {
  58.     /*if (response.indexOf("Wiktionary does not yet have an entry for") == -1) { // See comments at the end of the function*/
  59.         this.WL._mainLanguage=WikiLookdb_pref.getCharPref("WikiLookMainLanguage");
  60.         var pattern=' href="/w/index.php?title=';
  61.         var titleStarts=response.indexOf(pattern)
  62.         var TitleName=response.substring(titleStarts+pattern.length, response.indexOf("&action=",titleStarts+pattern.length));
  63.         //TitleName=unescape(TitleName);
  64.         var lngName="";
  65.         var lngStarts='<span class="mw-headline" id="';
  66.         var mlngstrStarts=lngStarts+this.WL._mainLanguage+'">';
  67.         var mlngstrStops='<h2><span class="editsection">[<a ';
  68.         var mlngStart=response.indexOf(mlngstrStarts)+mlngstrStarts.length+1;
  69.         if (mlngStart-mlngstrStarts.length-1==-1) {
  70.             mlngStart=response.indexOf(mlngstrStops);
  71.         } else {this.WL._mainLngFound=true;}
  72. /*        if (this.WL._goForIt==true&&this.WL._defFound==true) {
  73.             this.formOf();
  74.             return true;
  75.         }*/
  76.         var mlngStops=response.indexOf(mlngstrStops, mlngStart+1);
  77.         if (this.WL._mainLngFound){
  78.             if (mlngStops!=-1) { //if main language starts and stops, grab the section
  79.                 response=response.substring(mlngStart-1,mlngStops);
  80.             } else { //if it only starts, grab HTML from start to end of page
  81.                 response=response.substring(mlngStart-1,response.length);
  82.             }
  83.             lngName=response.substring(0,response.indexOf('<'));
  84.         } else { //If no main language detected, grab first languadge on the page
  85.             if (response.indexOf(mlngstrStops,response.indexOf(lngStarts))!=-1) {
  86.                 response=response.substring(response.indexOf(lngStarts)+lngStarts.length , response.indexOf(mlngstrStops,response.indexOf(lngStarts)));
  87.             } else {
  88.                 response=response.substring(response.indexOf(lngStarts)+lngStarts.length , response.length);
  89.             }    
  90.             lngName=response.substring(0,response.indexOf('">'));
  91.         }
  92. ////////////////////////
  93.         this.WL._outText = "";
  94.         var sectionName = "";
  95.         var defFoundNow = false;
  96.         var secIdx=0;
  97.         for(secIdx=0; secIdx < WL_Wiktionary_section_names.length; secIdx++) {
  98.             sectionName = WL_Wiktionary_section_names[secIdx];
  99.             r = false;
  100.             r = this.wiktionaryGetSection(response,sectionName);
  101.             if (r != false) {
  102.                 this.WL._outText += "<b>"+sectionName+"</b>"+r;
  103.                 defFoundNow = true;
  104.             }
  105.         }
  106.         if (this.WL._defFound) {
  107.             this.WL._outText = this.WL.wiktionarySetStyle(this.WL._outText);
  108.             if (this.WL._outText!="") {this.WL._outTextForFormOf=this.WL._outText}
  109.             //if(!this.WL.attachDiv()) return;
  110.             tStem = "";
  111.             var tStem2="";
  112.             var audioStarts='<span class="unicode audiolink"> <a href="';
  113.             if (WikiLookdb_pref.getBoolPref("WikiLookAutoAudio") == true) { //
  114.                 if (response.indexOf(audioStarts)!=-1)    {
  115.                     tStem = response.substring(response.indexOf(audioStarts)+audioStarts.length, response.indexOf('" class="internal"', response.indexOf(audioStarts)+audioStarts.length));
  116.                     if (!this.WL._audioLinkShown) {
  117.                         tStem2= '<audio src="' + tStem + '" autoplay></audio>' + ''; 
  118.                     }
  119.                 }
  120.             }
  121.             if ((window.navigator.userAgent.indexOf("Firefox/3.0")==-1)&&(window.navigator.userAgent.indexOf("Firefox/2.0")==-1)){
  122.                 if (response.indexOf(audioStarts)!=-1)    {
  123.                     tStem = response.substring(response.indexOf(audioStarts)+audioStarts.length, response.indexOf('" class="internal"', response.indexOf(audioStarts)+audioStarts.length));
  124.                     if (!this.WL._audioLinkShown) {
  125.                         tStem2 = tStem2 + '<audio id="OGG" src="' + tStem + '" ></audio>' + '<a class="WikiLookTitle" href="javascript:void(0);"><img id="audioPlayback" style="border-style: none" title="Click here to hear how the word is pronounced." src="chrome://wikilook/skin/audio.png" onclick="document.getElementById(\'OGG\').play()"></a>'; 
  126.                         this.WL._audioLinkShown=true;
  127.                     }
  128.                 }
  129.             }
  130.             
  131.             var pronunciation = "";
  132.             var pronStarts='<span class="IPA">/';
  133.             if (response.indexOf(pronStarts)!=-1)    {
  134.                 pronunciation = 'IPA:<b><i> /' + response.substring(response.indexOf(pronStarts)+pronStarts.length, response.indexOf('/</span>', response.indexOf(pronStarts)+pronStarts.length)) + '/</b></i>';
  135.             }
  136.             /*pronStarts='<tt class="SAMPA">/'; //should we include SAMPA? Not sure. If yes, please uncomment.
  137.             if (response.indexOf(pronStarts)!=-1)    {
  138.                 pronunciation = pronunciation +'SAMPA:<b><i> ' + response.substring(response.indexOf(pronStarts)+pronStarts.length, response.indexOf('/</tt>', response.indexOf(pronStarts)+pronStarts.length)) + '</b></i>, ';
  139.             }*/
  140.             pronStarts='<span class="AHD enPR">';
  141.             if (response.indexOf(pronStarts)!=-1)    {
  142.                 pronunciation = pronunciation + ', enPR:<b><i> ' + response.substring(response.indexOf(pronStarts)+pronStarts.length, response.indexOf('</span>', response.indexOf(pronStarts)+pronStarts.length)) + '</b></i>';
  143.             }
  144.             if (pronunciation.length>2) {pronunciation = '( ' + pronunciation + ' )'}
  145.             /*if (this.WL._thisIsAFormOf==false) {
  146.                 this.WL._div.style.left = this.WL.getNewWindowX() + "px";
  147.                 this.WL._div.style.top =this.WL.getNewWindowY() + "px";
  148.             }*/
  149.             // Next line is needed so redirects from proper noun that starts with lower case are shown correctly, while not touching the way words in weird alphabets are shown, like ones in Amharic.
  150.             //if (this.WL._currentWord.length==TitleName.length) {this.WL._currentWord=TitleName}
  151.             if ((defFoundNow==true&&lngName=="English"&&this.WL._engDefFound==false&&this.WL._mainLngFound==false)||(defFoundNow==true&&this.WL._mainLngFound==true)||(defFoundNow==true&&this.WL._divIsDisplayed==false)||(defFoundNow==true&&this.WL._thisIsAFormOf==true)){ 
  152.                 var copyOfCurrentWord="";
  153.                 if (this.WL._currentWord==null) {copyOfCurrentWord=TitleName;} else {copyOfCurrentWord=this.WL._currentWord}
  154.                 this.WL._divIsDisplayed = true;
  155.                 if (this.WL._thisIsAFormOf==false) { 
  156.                     this.WL._div.innerHTML = this.WL._CSS+"<a class='WikiLookTitle' href='javascript:void(0);'><img id='confDialog' style='border-style: none' title='Click here for configuration dialog.' src='chrome://wikilook/skin/config.png'></a>    "; 
  157.                 } else {
  158.                     this.WL._div.innerHTML =this.WL._div.innerHTML.replace(" autoplay", " ") +'<center><hr width="80%" align="center" border="2" noshade color="#F5F5F5"><\center><\hr><br>'; 
  159.                 }
  160.                 this.WL._div.innerHTML =this.WL._div.innerHTML + "<a class='WikiLookTitle' style=\"color:"+this.WL._linkColor+";\" href='http://en.wiktionary.org/wiki/"+copyOfCurrentWord+"' style='font-size:12px;'><span class='popup_mainlink'>"+decodeURI(copyOfCurrentWord)/*.replace("%27","\'")*/.replace(/_/g," ")+"</span></a> "
  161.                                 + "  " + tStem2 + "   "
  162.                                 +'<b><i><font size=-5 color=#660033>'+lngName.replace(/_/g," ")+' </font></b></i> ' + pronunciation 
  163.                                 +""
  164.                                 +" <br/><br/>"
  165.                                 +"<span style=\"display:block;font-family:arial;\">"
  166.                                 +this.WL._outText
  167.                                 +"</span>";
  168.                                 +"(retrieved from <a style=\"color:"+this.WL._linkColor+";\" href=http://www.wiktionary.org>wiktionary.org</a>)";
  169.             }
  170.             if(this.WL._defFound==true&&lngName=="English"&&this.WL._engDefFound==false) {this.WL._engDefFound=true;}
  171.             if((this.WL._goForIt==true&&this.WL._defFound==true)||(this.WL._mainLngFound==true&&this.WL._defFound==true)){
  172.                 //this.WL.showDiv();
  173.                 this.formOf();
  174.                 return true;
  175.             }
  176.         } 
  177.         return false;
  178.     /* FIXME This logic should use internal search suggestions of Wiktionary. CAREFUL! It could lead to empty articles!
  179.         } else { 
  180.         if (response.indexOf('<li><big>Did you mean <b><span id="did-you-mean"><a href="/wiki/')!=-1) {
  181.             var a=response.indexOf('<li><big>Did you mean <b><span id="did-you-mean"><a href="/wiki/');
  182.             var pattern='><em>';
  183.             var titleStarts=response.indexOf(pattern,a)
  184.             var TitleName=response.substring(a, response.indexOf(pattern,a+a.length));
  185.             this.WL._isRequestFetching = true;
  186.             this.WL._currentWord=TitleName;
  187.             this.WL.requestFetchWord2(TitleName);
  188.             return true;
  189.         }
  190.         return false;    
  191.     }*/
  192.     return false;
  193. }
  194.  
  195. WikiLook_en.prototype.wiktionaryGetSection = function (sourceText,sectionName) {
  196.     // Convert to lowercase to find pattern position.
  197.     // Discard lowercase needle and hay later.
  198.     var sNameLower = sectionName.replace(" ","_","ig"); //.toLowerCase();
  199.     var patternLower = 'class="mw-headline" id="' + sNameLower;
  200.     //+ '">' + sectionName + '</span></h';
  201.     var sourceLower = sourceText;//.toLowerCase();
  202.     var headPos = sourceLower.indexOf(patternLower);
  203.     if ( headPos != -1 ) {
  204.         this.WL._defFound = true;
  205.         var u = sourceText.substring(headPos+patternLower.length);
  206.         u = this.WL.cutHead_preserve("<ol>", u);
  207.         if (u==null) {return false;}
  208.         u = this.WL.cutTail_preserve("</ol>", u);
  209.         //u = u.replace(" href=\""," href=\"http://en.wiktionary.org", "gi");
  210.         return u;
  211.     }
  212.     return false;
  213. }
  214.  
  215.  
  216. /**
  217.  * This checks the current entry to see if it is a "form of" another word.
  218.  * If it is then we download the word of which it is a form and append that
  219.  * to the display.
  220. **/
  221. WikiLook_en.prototype.formOf = function () {
  222.  
  223.     //alert(this.WL._outTextForFormOf);
  224.     
  225.     //if (this.WL._outTextForFormOf.indexOf("spelling of")!=-1) {alert(this.WL._outTextForFormOf.substring(this.WL._outTextForFormOf.indexOf("spelling of"),this.WL._outTextForFormOf.indexOf("spelling of")+350));}
  226.     
  227.     //Only allow an extra two formOf's to be added
  228.     if (this.WL._thisIsAFormOfFormOf == true) 
  229.       return;
  230.       
  231.     if (this.WL._thisIsAFormOf == true) 
  232.       this.WL._thisIsAFormOfFormOf = true;
  233.  
  234.     var wordtemp="";
  235.     var word="";
  236.     
  237.     //This is the current contents of the form of templates, lets hope they don't change
  238.     var pattern = 'of <span class="mention"><a class="WikiLook" href="http://en.wiktionary.org/wiki/';
  239.        
  240.     //Extract the word of which we are a form
  241.     var a = this.WL._outTextForFormOf.indexOf(pattern);
  242.     
  243.     
  244.     if (a!=-1) {
  245.         word=this.WL._outTextForFormOf.substring(a+pattern.length, this.WL._outTextForFormOf.indexOf('" title="',a));
  246.         wordtemp=word.substring(0,word.indexOf("#"));
  247.     }
  248.     
  249.     if (word!="") {
  250.         if (word.length>wordtemp.length&&wordtemp.length>0){word=wordtemp;}
  251.         this.WL._thisIsAFormOf=true;
  252.         this.WL._currentWord=word;
  253.         this.WL.requestFetchWord2(word);
  254.         return;
  255.     }
  256.     //Repeat for other forms of word that we've found
  257.  
  258.     word="";
  259.     wordtemp="";
  260.     pattern = 'state of being <a class="WikiLook" href="http://en.wiktionary.org/wiki/';
  261.     var a = this.WL._outTextForFormOf.indexOf(pattern);
  262.     if (a!=-1) {
  263.         word=this.WL._outTextForFormOf.substring(a+pattern.length, this.WL._outTextForFormOf.indexOf('" title="',a));
  264.         wordtemp=word.substring(0,word.indexOf("#"));
  265.     }
  266.     if (word!="") {
  267.         if (word.length>wordtemp.length&&wordtemp.length>0){word=wordtemp;}
  268.         this.WL._thisIsAFormOf=true;
  269.         this.WL._currentWord=word;
  270.         this.WL.requestFetchWord2(word);
  271.         return;
  272.     }
  273.         
  274.     word="";
  275.     wordtemp="";
  276.     pattern = 'form of <a class="WikiLook" href="http://en.wiktionary.org/wiki/';
  277.     var a = this.WL._outTextForFormOf.indexOf(pattern);
  278.     if (a!=-1) {
  279.         word=this.WL._outTextForFormOf.substring(a+pattern.length, this.WL._outTextForFormOf.indexOf('" title="',a));
  280.         wordtemp=word.substring(0,word.indexOf("#"));
  281.     }
  282.     if (word!="") {
  283.         if (word.length>wordtemp.length&&wordtemp.length>0){word=wordtemp;}
  284.         this.WL._thisIsAFormOf=true;
  285.         this.WL._currentWord=word;
  286.         this.WL.requestFetchWord2(word);
  287.         return;
  288.     }
  289.     
  290.     word="";
  291.     wordtemp="";
  292.     pattern = 'form of <b class="WikiLook"><a class="WikiLook" href="http://en.wiktionary.org/wiki/';
  293.     var a = this.WL._outTextForFormOf.indexOf(pattern);
  294.     if (a!=-1) {
  295.         word=this.WL._outTextForFormOf.substring(a+pattern.length, this.WL._outTextForFormOf.indexOf('" title="',a));
  296.         wordtemp=word.substring(0,word.indexOf("#"));
  297.     }
  298.     if (word!="") {
  299.         if (word.length>wordtemp.length&&wordtemp.length>0){word=wordtemp;}
  300.         this.WL._thisIsAFormOf=true;
  301.         this.WL._currentWord=word;
  302.         this.WL.requestFetchWord2(word);
  303.         return;
  304.     }
  305.  
  306. }
  307.